#include "Server.h"
#include "boost\bind.hpp"

CServer::CServer(short port, int thread_cnt)
	: m_io_service_pool(thread_cnt)
	, m_acceptor(m_io_service_pool.get_io_service(), ip::tcp::endpoint(ip::tcp::v4(), port))
{
	CServerSession * pSession = new CServerSession(
		m_io_service_pool.get_io_service());
	m_acceptor.async_accept(pSession->socket(),
		bind(&CServer::handle_accept, this, pSession, asio::placeholders::error));
}


CServer::~CServer()
{
}

void CServer::handle_accept(CServerSession* pSession, const boost::system::error_code& err)
{
	if ( !err)
	{
		pSession->start();
	}
	else
	{
		delete pSession;
	}
	pSession = new CServerSession(m_io_service_pool.get_io_service());
	m_acceptor.async_accept(pSession->socket(),
		bind(&CServer::handle_accept, this, pSession, asio::placeholders::error));

}

void CServer::run()
{
	m_io_service_pool.start();
	m_io_service_pool.join();
}
